<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  fetchScheduleList,
  createSchedule,
  updateSchedule,
  deleteSchedule,
  refreshTask,
  refreshSchedule,
  changeJobStatus,
  runAJobNow
} from '@/api/tool/task/schedule'
import { useDictStore } from '@/stores/dict'

const dictStore = useDictStore()

const tableData = ref([])
const loading = ref(false)
const total = ref(0)

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  jobName: '',
  jobGroup: '',
  status: ''
})

// 获取任务列表
const getList = async () => {
  loading.value = true
  try {
    const res = await fetchScheduleList(queryParams.value)
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error('获取任务列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 刷新任务
const handleRefreshTask = async () => {
  try {
    await refreshTask()
    ElMessage.success('刷新任务成功')
    getList()
  } catch (error) {
    console.error('刷新任务失败:', error)
  }
}

// 刷新调度
const handleRefreshSchedule = async () => {
  try {
    await refreshSchedule()
    ElMessage.success('刷新调度成功')
    getList()
  } catch (error) {
    console.error('刷新调度失败:', error)
  }
}

// 改变任务状态
const handleChangeStatus = async (row) => {
  try {
    await changeJobStatus(row.id, row.status === '0' ? '1' : '0')
    ElMessage.success('修改状态成功')
    getList()
  } catch (error) {
    console.error('修改状态失败:', error)
  }
}

// 立即执行
const handleRunNow = async (row) => {
  try {
    await runAJobNow(row.id)
    ElMessage.success('执行成功')
  } catch (error) {
    console.error('执行失败:', error)
  }
}

onMounted(() => {
  getList()
})
</script>

<template>
  <div class="schedule-container">
    <div class="search-bar">
      <el-form :model="queryParams" inline>
        <el-form-item label="任务名称">
          <el-input v-model="queryParams.jobName" placeholder="请输入任务名称" clearable />
        </el-form-item>
        <el-form-item label="任务分组">
          <el-input v-model="queryParams.jobGroup" placeholder="请输入任务分组" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in dictStore.getDict('status')"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button @click="queryParams = { pageNum: 1, pageSize: 10 }">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="toolbar">
      <el-button type="primary" @click="handleRefreshTask">刷新任务</el-button>
      <el-button type="success" @click="handleRefreshSchedule">刷新调度</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column prop="jobName" label="任务名称" />
      <el-table-column prop="jobGroup" label="任务分组" />
      <el-table-column prop="cronExpression" label="cron表达式" />
      <el-table-column prop="status" label="状态">
        <template #default="{ row }">
          <el-tag :type="row.status === '0' ? 'success' : 'danger'">
            {{ row.status === '0' ? '正常' : '停止' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button
            type="primary"
            link
            @click="handleChangeStatus(row)"
          >
            {{ row.status === '0' ? '停止' : '启动' }}
          </el-button>
          <el-button
            type="primary"
            link
            @click="handleRunNow(row)"
          >
            执行
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="getList"
        @current-change="getList"
      />
    </div>
  </div>
</template>

<style scoped>
.schedule-container {
  padding: 20px;
}

.search-bar {
  margin-bottom: 20px;
  padding: 20px;
  background: var(--bg-primary);
  border-radius: 4px;
}

.toolbar {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
